/*
 * Copyright 2010 Yannick Stucki (yannickstucki.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yannickstucki.android.musicqueue.ui;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.yannickstucki.android.musicqueue.communication.ISongEnqueuer;
import com.yannickstucki.android.musicqueue.communication.OnContextMenuClickedListener;
import com.yannickstucki.android.musicqueue.communication.OnUiUpdateListener;
import com.yannickstucki.android.musicqueue.data.Song;

/**
 * In this screen mode the user can enqueue songs or change the current song.
 * 
 * This class is a singleton.
 * 
 * @author Yannick Stucki
 * 
 */
public class ChoosingScreenMode implements IScreenMode, OnContextMenuClickedListener {

  /**
   * Adapter to display the songs in the list view.
   */
  private SongAdapter songAdapter;

  /**
   * Adapter to display the starred songs in the list view.
   */
  private SongAdapter starredSongAdapter;

  /**
   * The choosing screen needs to be able to enqueue songs.
   */
  private ISongEnqueuer songEnqueuer;
  
  /**
   * Reference to the listView.
   */
  private ListView listView;
  
  /**
   * The only constructor.
   */
  public ChoosingScreenMode(ISongEnqueuer songEnqueuer, SongAdapter songAdapter,
      SongAdapter starredSongAdapter) {
    this.songAdapter = songAdapter;
    this.starredSongAdapter = starredSongAdapter;
    this.songEnqueuer = songEnqueuer;
  }

  /**
   * The screen needs all the ui elements to work.
   */
  public void setUiElements(final ListView listView, ImageButton showSongs,
      ImageButton showStarredSongs) {
    
    Song.currentDisplayStringIndex = Song.DISPLAY_INDEX_LANDSCAPE;   
    
    this.listView = listView;
    listView.setAdapter(songAdapter);
    listView.setFooterDividersEnabled(false);
    listView.setTextFilterEnabled(true);
    listView.setFastScrollEnabled(true);
    
    showSongs.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        listView.setAdapter(ChoosingScreenMode.this.songAdapter);
      }
    });

    showStarredSongs.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        listView.setAdapter(ChoosingScreenMode.this.starredSongAdapter);
      }
    });
    
    listView.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Song song = (Song) parent.getItemAtPosition(position);
        songEnqueuer.enqueue(song);
      }
    });
  }

  @Override
  public OnUiUpdateListener getOnUiUpdateListener() {
    return null; // Not interested in reacting to music change.
  }

  @Override
  public void onPlayNowClicked(int position) {
    songEnqueuer.playNow((Song) listView.getItemAtPosition(position));
  }
}
